package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosLinkmanDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.ExtOrderInfoCache;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.ChangeTypeUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 组装报文客户联系人节点
 *
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl <br>
 */
@Service
public class BcmcPosLinkmanHandler extends AbstractBcmcPosHandler {

    @Override
    public void doHandle(BcmcPosContext context) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst kiteSceneInst = CuccPosServiceUtils.getSceneInst(context);

        if (StringUtils.equalsAny(kiteSceneInst.getOperType(), LocalKeyConsts.SCENE_OPER_TYPE_ORDER_SIGN,
            LocalKeyConsts.SCENE_OPER_TYPE_TRANSFER_PLAN_MODIFY)) {
            return;
        }

        // 获取客户联系人属性
        List<KiteAttr> developerAttrs = querySceneInstService.listCommonAttrsByModuleId(context.getSceneInstId(),
            LocalKeyConsts.MODULE_ID_CONTACT);

        if (KiteListUtils.isEmpty(developerAttrs)) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("未配置【客户联系人】属性");
        }

        KitePosLinkmanDTO linkman = genKitePosLinkmanDTO(context, developerAttrs);
        // 设置操作类型
        linkman.setChangeType(getChangeType(context, linkman));
        context.getPosDto().setLinkman(linkman);
    }

    private KitePosLinkmanDTO genKitePosLinkmanDTO(BcmcPosContext context, List<KiteAttr> developerAttrs) {
        KitePosLinkmanDTO linkman = new KitePosLinkmanDTO();
        linkman.setName(this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_NAME, "未配置【客户联系人名称】属性"));
        linkman.setPhone(this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_TEL, "未配置【客户联系人电话】属性"));
        linkman.setEmail(this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_EMAIL, "未配置【客户联系人邮箱】属性"));
        linkman.setAddress(this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_ADDRESS, "未配置【客户联系人地址】属性"));
        linkman.setFax(this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_FAXNUM, "未配置【客户联系人传真号码】属性"));
        linkman.setFixedPhone(
            this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_FIXEDPHONE, "未配置【客户联系人固话号码】属性"));
        linkman.setPostcode(this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_POSTCODE, "未配置【客户联系人邮编】属性"));
        linkman.setPersonIdType(
            this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_CARDTYPE, "未配置【客户联系人证件类型】属性"));
        linkman.setRemark(this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_COMMENTS, "未配置【客户联系人备注】属性"));
        // 传了空字符串会校验
        if (KiteStringUtils.isEmpty(linkman.getPersonIdType())) {
            linkman.setPersonIdType(null);
        }
        linkman
            .setPersonNum(this.getAttrValue(developerAttrs, LocalKeyConsts.CONTACT_INFO_CARDNUM, "未配置【客户联系人证件号码】属性"));
        return linkman;
    }

    /**
     * Description: <br>
     * 获取变更类型
     *
     * @return <br>
     */
    private String getChangeType(BcmcPosContext context, KitePosLinkmanDTO linkman) {
        String changeType = ChangeTypeUtils.getChangeType(ActionType.A.name());
        KitePosDTO posDto = ExtOrderInfoCache.get(context.getSceneInstId());
        if (Objects.isNull(posDto)) {
            return changeType;
        }
        // 对比客户联系人属性是否存在变更
        KitePosLinkmanDTO oldLinkman = posDto.getLinkman();
        Set<String> difAttrSet = KiteObjectUtils.compareObjectFields(linkman, oldLinkman);

        if (KiteObjectUtils.compareObject(linkman, oldLinkman)) {
            changeType = ChangeTypeUtils.getChangeType(ActionType.K.name());
        }
        else {
            changeType = ChangeTypeUtils.getChangeType(ActionType.M.name());
        }
        return changeType;
    }
}
